package com.leetcodehot.problems;

import java.util.ArrayList;
import java.util.List;

public class problems216 {
    private int k;
    private int n;
    private List<List<Integer>> res = new ArrayList<>();
    private List<Integer> path = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int _k, int _n) {
        n = _n;
        k = _k;
        dfs(9, n);
        return res;
    }

    private void dfs(int i, int t) {
        int d = k - path.size();
        if (t < 0 || t > (i * 2 - d + 1) * d / 2) {
            return;
        }

        if (d == 0) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int j = i; j >= d; j--) {
            path.add(j);
            dfs(j - 1, t - j);
            path.remove(path.size() - 1);
        }
    }
}
